package com.eric.windows.imgDispose.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;


import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EView;

/**
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</h3> 2017/8/31 10:59
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLIT  Co., Ltd. Inc. All rights reserved.
 */
@EView
public class CanvasView extends View {

    private int width = 400;
    private  int height = 400;
    private Paint mPaint;
    private Bitmap dstBmp;
    private  Bitmap srcBmp;

    public CanvasView(Context context) {
        super(context);
    }

    public CanvasView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CanvasView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CanvasView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @AfterInject
    void initData(){
        mPaint = new Paint();
        mPaint.setStrokeWidth(5);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);

        getDstBmp();
        getSrcBmp();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        int layerID = canvas.saveLayer(0, 0, width * 2, height * 2, mPaint, Canvas.ALL_SAVE_FLAG);
        /**
         * 参数二 三 要绘制的bitmap的左顶点，即（left，top）
         */
        canvas.drawBitmap(srcBmp, 100, 100, mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(dstBmp, width / 2, height / 2, mPaint);

    }

    private void getDstBmp(){
        mPaint.setColor(Color.RED);
        dstBmp = Bitmap.createBitmap(width ,height , Bitmap.Config.ARGB_8888);
        Canvas  dstCanvas = new Canvas(dstBmp);
        dstCanvas.drawRect(0,0,width,height,mPaint);

    }

    private void  getSrcBmp(){
        mPaint.setColor(0xFFFFCC44);
        /**
         * 参数一：要创建的bitmap的宽度
         * 参数二：要创建的bitmap的高度
         * 参数三：bitmap的配置，
         */
        srcBmp = Bitmap.createBitmap(width ,height , Bitmap.Config.ARGB_8888);
        Canvas  srcCanvas = new Canvas(srcBmp);
        /**
         *
         * 参数一 二 三 四 分别是矩形的left top right bottom ，即（l，t），（r，b）来确定矩形，
         * 但此时的坐标不是屏幕的，而是bitmap的坐标
         *
         */
        srcCanvas.drawCircle(width/2,height/2,width/2,mPaint);

    }

}
